import 'package:flutter/material.dart';
import 'home/Home.dart';
import 'test/Test.dart';
import 'test/LoadMorePage.dart';
import 'study/Study.dart';
import 'mine/Mine.dart';
import 'package:provider/provider.dart';
import '../provider/CurrentIndexProvider.dart';
import '../provider/UserProvider.dart';
import '../../utils/Global.dart';
class Index extends StatefulWidget{
  const Index({Key? key}) : super(key: key);
  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> {
  // 1.声明pageController
  late PageController _pageController;
  final List<BottomNavigationBarItem> bottomNavItems = [
    // BottomNavigationBarItem(
    //   icon: Icon(Icons.home),
    //   backgroundColor: Colors.purple[100],
    //   label: '滑动列表'
    // ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      backgroundColor: Colors.purple[100],
      label: '首页'
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.purple[100],
      icon: Icon(Icons.message),
      label: '学习'
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.purple[100],
      icon: Icon(Icons.person),
      label: '我'
    )
  ];
  final List pages = [
    // {
    //   "appBar": AppBar(
    //     automaticallyImplyLeading: false,
    //     actions: [],
    //     title: Text('滑动列表'),
    //     centerTitle: true,
    //     elevation: 0,
    //   ),
    //   'page': LoadMorePage()
    // },
    {
      "appBar": AppBar(
        automaticallyImplyLeading: false,
        actions: [],
        title: Text('首页'),
        centerTitle: true,
        elevation: 0,
      ),
      'page': Home()
    },
    {
      "appBar": AppBar(
        actions: [],
        automaticallyImplyLeading: false,
        title: Text('学习中心'),
        centerTitle: true,
        elevation: 0,
      ),
      'page': Study()
    },
    {
      "appBar": AppBar(
        actions: [],
        automaticallyImplyLeading: false,
        title: Text('个人中心'),
        centerTitle: true,
        elevation: 0,
      ),
      'page': Mine()
    }
  ];
  // int currentIndex = 0;

  @override
  void initState() {
    // 2.初始化PageController
    _pageController = PageController(
      initialPage: G.getCurrentContext()!.watch<CurrentIndexProvider>().currentIndex
    );
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // 使用组件共享的Provider来定义currentIndex
    int currentIndex = Provider.of<CurrentIndexProvider>(context).currentIndex;
    // 判断用户是否登录校验
    UserProvider userProvider = Provider.of<UserProvider>(context);
    return Scaffold(
      appBar: pages[currentIndex]['appBar'],
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        selectedItemColor: Colors.lightBlue,
        type: BottomNavigationBarType.fixed, // 菜单类型 浮动和固定
        onTap: (value) async {
          // setState(() {
          //   currentIndex = value;
          // });

          // 默认是监听调用 listen传false 可以解决报错 监听调用只能在StatelessWidget.build 和 State.build中使用
          
          if(value == 1 || value == 2) {
            if(userProvider.isLogin == false) {
              G.router?.navigateTo(context, '/login');
              return;
            } else {
              Map userInfo = await G.api.user.getUerInfo();
              print(userInfo);
              userProvider.setUserInfo(userInfo);  
            }
          }
          // Provider.of<CurrentIndexProvider>(context, listen: false).changeIndex(value);
          context.read<CurrentIndexProvider>().changeIndex(value);
          // 4. 跳转到指定页面
          setState(() {
            _pageController.jumpToPage(value);
          });
        },
      ),
      // body: pages[currentIndex]['page'],
      // body: IndexedStack(
      //   index: currentIndex,
      //   children: pages.map<Widget>((e) => e['page']).toList(),
      // ),
      // 3.将body改为pageView
      body: PageView(
        controller: _pageController,
        children: pages.map<Widget>((e) => e['page']).toList(),
      ),
    )
    ;
  }
}